package com.hcb.tonguetiphealth.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader;
import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader.ImageCallBack;
import com.hcb.tonguetiphealth.model.Restaurant;
import com.hcb.tonguetiphealth.view.R;

public class ResListAdapter extends BaseAdapter {

	private Context mContext;
	private List<Restaurant> mList;
	private LayoutInflater vi;
	private AsyncBitmapLoader asyncBitmapLoader;
	
	public ResListAdapter(Context context,List<Restaurant> list) {
		mContext = context;
		mList = list;
		asyncBitmapLoader = AsyncBitmapLoader.getInstance();
	}
	
	public int getCount() {
		if(mList == null) {
			return 0;
		}
		return mList.size();
	}
	
	public Object getItem(int position) {
		if(mList == null) {
			return null;
		} else {
			return mList.get(position);
		}
	}
	
	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {

		View view;
		
		if(convertView == null) {
			vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = (LinearLayout) vi.inflate(R.layout.res_book_list_item, null);
		} else {
			view = convertView;
		}
		FillDataIntoView(view, position);
		
		return view;
	}
	
	private void FillDataIntoView(View view, int position) {
		final Restaurant res = mList.get(position);

		ImageView img = (ImageView)view.findViewById(R.id.res_img);
		TextView tv_name = (TextView)view.findViewById(R.id.res_name_textview);
		RatingBar bar = (RatingBar)view.findViewById(R.id.res_level_ratings_ratingbar);
		TextView tv_debate = (TextView)view.findViewById(R.id.res_debate_textview);
		TextView tv_avg = (TextView)view.findViewById(R.id.res_avg_textview );
		TextView tv_label = (TextView)view.findViewById(R.id.res_label_textview);
		TextView tv_dis = (TextView)view.findViewById(R.id.res_distance_textview);
		//img.setBackgroundResource(R.drawable.menu_entrance);
		tv_name.setText(res.getName());
		bar.setRating(res.getLevel());
		tv_debate.setText(res.getRebate());
		tv_avg.setText(res.getAvg()+ " 元");
		String labels = res.getLabel();
//		if (labels !=null && !labels.equals("")) {//换行
//			String labelsArray[] = labels.split(" ");
//			if (labelsArray.length >=3) {
//				labels = "";
//				for (int i = 0; i < labelsArray.length; i++) {
//					labels += labelsArray[i] + " ";
//					if (i == 1 ) {
//						labels += "\n";
//					}
//				}
//			}
//		}
		tv_label.setText(labels);
		tv_dis.setText(res.getDistance() + " 米");
		
		String imageURL = res.getImgURL(); 
        Bitmap bitmap = asyncBitmapLoader.loadBitmap(img, imageURL, new ImageCallBack() {  
              
            public void imageLoad(ImageView imageView, Bitmap bitmap) {  
                // TODO Auto-generated method stub  
                imageView.setImageBitmap(bitmap);  
            }  
        });  
		if(bitmap == null) {    
			img.setImageResource(R.drawable.ic_launcher);    
		} else {    
		    img.setImageBitmap(bitmap);    
		}
	    
	}
}
